package com.ruoyi.project.edu.stuCourse.controller;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.web.page.PageDomain;
import com.ruoyi.framework.web.page.TableSupport;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.edu.stuCourse.domain.StuCourse;
import com.ruoyi.project.edu.stuCourse.service.IStuCourseService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 学生课程Controller
 *
 * @author yueqiangu
 * @date 2021-02-27
 */
@Controller
@RequestMapping("/edu/stuCourse")
public class StuCourseController extends BaseController
{
    private String prefix = "edu/stuCourse";

    @Autowired
    private IStuCourseService stuCourseService;

    @RequiresPermissions("edu:stuCourse:view")
    @GetMapping()
    public String stuCourse()
    {
        return prefix + "/stuCourse";
    }


//    @RequiresPermissions("edu:stuCourse:choice")
    @GetMapping("/choice")
    public String choice(ModelMap mmap)
    {
        String loginName = ShiroUtils.getLoginName();
        mmap.put("userName",loginName);
        return prefix + "/choice";
    }


    /**
     * 查询学生课程列表
     */
//    @RequiresPermissions("edu:stuCourse:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(StuCourse stuCourse)
    {
        startPage();
        List<StuCourse> list = stuCourseService.selectStuCourseList(stuCourse);
        TableDataInfo tableDataInfo = getDataTable(list);
        Long t = tableDataInfo.getTotal();
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        Integer total = Integer.parseInt(Long.toString(tableDataInfo.getTotal()));
        int totalPage = (int) (total / pageSize + ((total % pageSize == 0) ? 0 : 1));
        List<Integer> pageArray = new ArrayList<>();
        for(int i =1 ;i<totalPage+1;i++){
            pageArray.add(i);
        }
        tableDataInfo.setPageArray(pageArray);
        tableDataInfo.setPageNum(pageNum);
        tableDataInfo.setPageSize(pageSize);
        return tableDataInfo;
    }

    /**
     * 导出学生课程列表
     */
    @RequiresPermissions("edu:stuCourse:export")
    @Log(title = "学生课程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(StuCourse stuCourse)
    {
        List<StuCourse> list = stuCourseService.selectStuCourseList(stuCourse);
        ExcelUtil<StuCourse> util = new ExcelUtil<StuCourse>(StuCourse.class);
        return util.exportExcel(list, "stuCourse");
    }

    /**
     * 新增学生课程
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存学生课程
     */
//    @RequiresPermissions("edu:stuCourse:add")
    @Log(title = "学生课程", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StuCourse stuCourse)
    {
        return toAjax(stuCourseService.insertStuCourse(stuCourse));
    }

    /**
     * 修改学生课程
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        StuCourse stuCourse = stuCourseService.selectStuCourseById(id);
        mmap.put("stuCourse", stuCourse);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生课程
     */
    @RequiresPermissions("edu:stuCourse:edit")
    @Log(title = "学生课程", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(StuCourse stuCourse)
    {
        return toAjax(stuCourseService.updateStuCourse(stuCourse));
    }

    /**
     * 删除学生课程
     */
    @RequiresPermissions("edu:stuCourse:remove")
    @Log(title = "学生课程", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(stuCourseService.deleteStuCourseByIds(ids));
    }
}
